package com.ycuwq.datepicker.time;

import com.ycuwq.datepicker.ResourceTable;
import com.ycuwq.datepicker.util.AttrUtils;
import com.ycuwq.datepicker.util.Utils;
import ohos.agp.components.AttrSet;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.element.Element;
import ohos.agp.utils.Color;
import ohos.app.Context;

/**
 * HourAndMinutePicker
 * Created by ycuwq on 2018/1/22.
 */
public class HourAndMinutePicker extends DirectionalLayout implements
        HourPicker.OnHourSelectedListener, MinutePicker.OnMinuteSelectedListener {
    /**
     * * The constant HourAndMinutePicker_itemTextSize
     */
    public static final String HOURANDMINUTEPICKER_ITEMTEXTSIZE = "itemTextSize";
    /**
     * * The constant HourAndMinutePicker_halfVisibleItemCount
     */
    public static final String HOURANDMINUTEPICKER_HALFVISIBLEITEMCOUNT = "halfVisibleItemCount";
    /**
     * * The constant HourAndMinutePicker_itemTextColor
     */
    public static final String HOURANDMINUTEPICKER_ITEMTEXTCOLOR = "itemTextColor";
    /**
     * * The constant HourAndMinutePicker_textGradual
     */
    public static final String HOURANDMINUTEPICKER_TEXTGRADUAL = "textGradual";
    /**
     * * The constant HourAndMinutePicker_selectedTextColor
     */
    public static final String HOURANDMINUTEPICKER_SELECTEDTEXTCOLOR = "selectedTextColor";
    /**
     * * The constant HourAndMinutePicker_selectedTextSize
     */
    public static final String HOURANDMINUTEPICKER_SELECTEDTEXTSIZE = "selectedTextSize";
    /**
     * * The constant HourAndMinutePicker_itemWidthSpace
     */
    public static final String HOURANDMINUTEPICKER_ITEMWIDTHSPACE = "itemWidthSpace";
    /**
     * * The constant HourAndMinutePicker_itemHeightSpace
     */
    public static final String HOURANDMINUTEPICKER_ITEMHEIGHTSPACE = "itemHeightSpace";
    /**
     * * The constant HourAndMinutePicker_zoomInSelectedItem
     */
    public static final String HOURANDMINUTEPICKER_ZOOMINSELECTEDITEM = "zoomInSelectedItem";
    /**
     * * The constant HourAndMinutePicker_wheelCyclic
     */
    public static final String HOURANDMINUTEPICKER_WHEELCYCLIC = "wheelCyclic";
    /**
     * * The constant HourAndMinutePicker_wheelCurtain
     */
    public static final String HOURANDMINUTEPICKER_WHEELCURTAIN = "wheelCurtain";
    /**
     * * The constant HourAndMinutePicker_wheelCurtainColor
     */
    public static final String HOURANDMINUTEPICKER_WHEELCURTAINCOLOR = "wheelCurtainColor";
    /**
     * * The constant HourAndMinutePicker_wheelCurtainBorder
     */
    public static final String HOURANDMINUTEPICKER_WHEELCURTAINBORDER = "wheelCurtainBorder";
    /**
     * * The constant HourAndMinutePicker_wheelCurtainBorderColor
     */
    public static final String HOURANDMINUTEPICKER_WHEELCURTAINBORDERCOLOR = "wheelCurtainBorderColor";
    /**
     * * The constant WheelItemTextSize
     */
    public static int WheelItemTextSize = 16;
    /**
     * * The constant com_ycuwq_datepicker_selectedTextColor
     */
    public static int com_ycuwq_datepicker_selectedTextColor = 0xff33aaff;
    /**
     * * The constant WheelSelectedItemTextSize
     */
    public static int WheelSelectedItemTextSize = 20;
    /**
     * * The constant WheelItemWidthSpace
     */
    public static int WheelItemWidthSpace = 32;
    /**
     * * The constant WheelItemHeightSpace
     */
    public static int WheelItemHeightSpace = 16;
    /**
     * * The constant com_ycuwq_datepicker_divider
     */
    public static int com_ycuwq_datepicker_divider = 0xffcccccc;
    /**
     * The constant M hour picker
     */
    private HourPicker mHourPicker;
    /**
     * The constant M minute picker
     */
    private MinutePicker mMinutePicker;
    /**
     * The constant M on time selected listener
     */
    private OnTimeSelectedListener mOnTimeSelectedListener;

    /**
     * Hour and minute picker
     *
     * @param context context
     */
    public HourAndMinutePicker(Context context) {
        this(context, null);
    }

    /**
     * Hour and minute picker
     *
     * @param context context
     * @param attrs   attrs
     */
    public HourAndMinutePicker(Context context, AttrSet attrs) {
        this(context, attrs, null);
    }

    /**
     * Hour and minute picker
     *
     * @param context      context
     * @param attrs        attrs
     * @param defStyleAttr def style attr
     */
    public HourAndMinutePicker(Context context, AttrSet attrs, String defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_layout_time, this, true);
        initChild();
        initAttrs(context, attrs);
        mHourPicker.setBackground(getBackgroundElement());
        mMinutePicker.setBackground(getBackgroundElement());
    }


    /**
     * On hour selected *
     *
     * @param hour hour
     */
    @Override
    public void onHourSelected(int hour) {
        onTimeSelected();
    }

    /**
     * On minute selected *
     *
     * @param hour hour
     */
    @Override
    public void onMinuteSelected(int hour) {
        onTimeSelected();
    }

    /**
     * Init attrs *
     *
     * @param context context
     * @param attrs   attrs
     */
    private void initAttrs(Context context, AttrSet attrs) {
        if (attrs == null) {
            return;
        }

        int textSize = AttrUtils.getDimensionFromAttr(attrs, HOURANDMINUTEPICKER_ITEMTEXTSIZE,
                Utils.dp2px(context, WheelItemTextSize));
        int textColor = AttrUtils.getColorFromAttr(attrs, HOURANDMINUTEPICKER_ITEMTEXTCOLOR,
                Color.BLACK.getValue());
        boolean isTextGradual = AttrUtils.getBooleanFromAttr(attrs, HOURANDMINUTEPICKER_TEXTGRADUAL, true);
        boolean isCyclic = AttrUtils.getBooleanFromAttr(attrs, HOURANDMINUTEPICKER_WHEELCYCLIC, true);
        int halfVisibleItemCount = AttrUtils.getIntFromAttr(attrs, HOURANDMINUTEPICKER_HALFVISIBLEITEMCOUNT, 2);
        int selectedItemTextColor = AttrUtils.getColorFromAttr(attrs, HOURANDMINUTEPICKER_SELECTEDTEXTCOLOR,
                com_ycuwq_datepicker_selectedTextColor);
        int selectedItemTextSize = AttrUtils.getDimensionFromAttr(attrs, HOURANDMINUTEPICKER_SELECTEDTEXTSIZE,
                Utils.sp2px(context, WheelSelectedItemTextSize));
        int itemWidthSpace = AttrUtils.getDimensionFromAttr(attrs, HOURANDMINUTEPICKER_ITEMWIDTHSPACE,
                Utils.dp2px(mContext, WheelItemWidthSpace));
        int itemHeightSpace = AttrUtils.getDimensionFromAttr(attrs, HOURANDMINUTEPICKER_ITEMHEIGHTSPACE,
                Utils.dp2px(mContext, WheelItemHeightSpace));
        boolean isZoomInSelectedItem = AttrUtils.getBooleanFromAttr(attrs, HOURANDMINUTEPICKER_ZOOMINSELECTEDITEM, true);
        boolean isShowCurtain = AttrUtils.getBooleanFromAttr(attrs, HOURANDMINUTEPICKER_WHEELCURTAIN, true);
        int curtainColor = AttrUtils.getColorFromAttr(attrs, HOURANDMINUTEPICKER_WHEELCURTAINCOLOR, Color.WHITE.getValue());
        boolean isShowCurtainBorder = AttrUtils.getBooleanFromAttr(attrs, HOURANDMINUTEPICKER_WHEELCURTAINBORDER, true);
        int curtainBorderColor = AttrUtils.getColorFromAttr(attrs, HOURANDMINUTEPICKER_WHEELCURTAINBORDERCOLOR,
                com_ycuwq_datepicker_divider);

        setTextSize(textSize);
        setTextColor(textColor);
        setTextGradual(isTextGradual);
        setCyclic(isCyclic);
        setHalfVisibleItemCount(halfVisibleItemCount);
        setSelectedItemTextColor(selectedItemTextColor);
        setSelectedItemTextSize(selectedItemTextSize);
        setItemWidthSpace(itemWidthSpace);
        setItemHeightSpace(itemHeightSpace);
        setZoomInSelectedItem(isZoomInSelectedItem);
        setShowCurtain(isShowCurtain);
        setCurtainColor(curtainColor);
        setShowCurtainBorder(isShowCurtainBorder);
        setCurtainBorderColor(curtainBorderColor);
    }

    /**
     * Init child
     */
    private void initChild() {
        mHourPicker = (HourPicker) findComponentById(ResourceTable.Id_hourPicker_layout_time);
        mHourPicker.setOnHourSelectedListener(this);
        mMinutePicker = (MinutePicker) findComponentById(ResourceTable.Id_minutePicker_layout_time);
        mMinutePicker.setOnMinuteSelectedListener(this);
    }

    /**
     * On time selected
     */
    private void onTimeSelected() {
        if (mOnTimeSelectedListener != null) {
            mOnTimeSelectedListener.onTimeSelected(getHour(), getMinute());
        }
    }


    /**
     * Sets time.
     *
     * @param hour   the year
     * @param minute the month
     */
    public void setTime(int hour, int minute) {
        setTime(hour, minute, true);
    }

    /**
     * Sets time.
     *
     * @param hour         the year
     * @param minute       the month
     * @param smoothScroll the smooth scroll
     */
    public void setTime(int hour, int minute, boolean smoothScroll) {
        mHourPicker.setSelectedHour(hour, smoothScroll);
        mMinutePicker.setSelectedMinute(minute, smoothScroll);
    }

    /**
     * Gets hour.
     *
     * @return the hour
     */
    public int getHour() {
        return mHourPicker.getCurrentPosition();
    }


    /**
     * Gets minuute.
     *
     * @return the minute
     */
    public int getMinute() {
        return mMinutePicker.getCurrentPosition();
    }

    /**
     * Set background *
     *
     * @param element element
     */
    @Override
    public void setBackground(Element element) {
        super.setBackground(element);
        if (mHourPicker != null) {
            mHourPicker.setBackground(element);
        }
        if (mMinutePicker != null) {
            mMinutePicker.setBackground(element);
        }
    }

    /**
     * Get hour picker hour picker
     *
     * @return the hour picker
     */
    public HourPicker getHourPicker() {
        return mHourPicker;
    }

    /**
     * Get minute picker minute picker
     *
     * @return the minute picker
     */
    public MinutePicker getMinutePicker() {
        return mMinutePicker;
    }

    /**
     * 一般列表的文本颜色
     *
     * @param textColor 文本颜色
     */
    public void setTextColor(int textColor) {
        mHourPicker.setTextColor(textColor);
        mMinutePicker.setTextColor(textColor);
    }

    /**
     * 一般列表的文本大小
     *
     * @param textSize 文字大小
     */
    public void setTextSize(int textSize) {
        mHourPicker.setTextSize(textSize);
        mMinutePicker.setTextSize(textSize);
    }

    /**
     * 设置被选中时候的文本颜色
     *
     * @param selectedItemTextColor 文本颜色
     */
    public void setSelectedItemTextColor(int selectedItemTextColor) {
        mHourPicker.setSelectedItemTextColor(selectedItemTextColor);
        mMinutePicker.setSelectedItemTextColor(selectedItemTextColor);
    }

    /**
     * 设置被选中时候的文本大小
     *
     * @param selectedItemTextSize 文字大小
     */
    public void setSelectedItemTextSize(int selectedItemTextSize) {
        mHourPicker.setSelectedItemTextSize(selectedItemTextSize);
        mMinutePicker.setSelectedItemTextSize(selectedItemTextSize);
    }


    /**
     * 设置显示数据量的个数的一半。
     * 为保证总显示个数为奇数,这里将总数拆分，itemCount = mHalfVisibleItemCount * 2 + 1
     *
     * @param halfVisibleItemCount 总数量的一半
     */
    public void setHalfVisibleItemCount(int halfVisibleItemCount) {
        mHourPicker.setHalfVisibleItemCount(halfVisibleItemCount);
        mMinutePicker.setHalfVisibleItemCount(halfVisibleItemCount);
    }

    /**
     * Sets item width space.
     *
     * @param itemWidthSpace the item width space
     */
    public void setItemWidthSpace(int itemWidthSpace) {
        mHourPicker.setItemWidthSpace(itemWidthSpace);
        mMinutePicker.setItemWidthSpace(itemWidthSpace);
    }

    /**
     * 设置两个Item之间的间隔
     *
     * @param itemHeightSpace 间隔值
     */
    public void setItemHeightSpace(int itemHeightSpace) {
        mHourPicker.setItemHeightSpace(itemHeightSpace);
        mMinutePicker.setItemHeightSpace(itemHeightSpace);
    }


    /**
     * Set zoom in center item.
     *
     * @param zoomInSelectedItem the zoom in center item
     */
    public void setZoomInSelectedItem(boolean zoomInSelectedItem) {
        mHourPicker.setZoomInSelectedItem(zoomInSelectedItem);
        mMinutePicker.setZoomInSelectedItem(zoomInSelectedItem);
    }

    /**
     * 设置是否循环滚动。
     * set wheel cyclic
     *
     * @param cyclic 上下边界是否相邻
     */
    public void setCyclic(boolean cyclic) {
        mHourPicker.setCyclic(cyclic);
        mMinutePicker.setCyclic(cyclic);
    }

    /**
     * 设置文字渐变，离中心越远越淡。
     * Set the text color gradient
     *
     * @param textGradual 是否渐变
     */
    public void setTextGradual(boolean textGradual) {
        mHourPicker.setTextGradual(textGradual);
        mMinutePicker.setTextGradual(textGradual);
    }


    /**
     * 设置中心Item是否有幕布遮盖
     * set the center item curtain cover
     *
     * @param showCurtain 是否有幕布
     */
    public void setShowCurtain(boolean showCurtain) {
        mHourPicker.setShowCurtain(showCurtain);
        mMinutePicker.setShowCurtain(showCurtain);
    }

    /**
     * 设置幕布颜色
     * set curtain color
     *
     * @param curtainColor 幕布颜色
     */
    public void setCurtainColor(int curtainColor) {
        mHourPicker.setCurtainColor(curtainColor);
        mMinutePicker.setCurtainColor(curtainColor);
    }

    /**
     * 设置幕布是否显示边框
     * set curtain border
     *
     * @param showCurtainBorder 是否有幕布边框
     */
    public void setShowCurtainBorder(boolean showCurtainBorder) {
        mHourPicker.setShowCurtainBorder(showCurtainBorder);
        mMinutePicker.setShowCurtainBorder(showCurtainBorder);
    }

    /**
     * 幕布边框的颜色
     * curtain border color
     *
     * @param curtainBorderColor 幕布边框颜色
     */
    public void setCurtainBorderColor(int curtainBorderColor) {
        mHourPicker.setCurtainBorderColor(curtainBorderColor);
        mMinutePicker.setCurtainBorderColor(curtainBorderColor);
    }

    /**
     * 设置选择器的指示器文本
     * set indicator text
     *
     * @param hourText   小时指示器文本
     * @param minuteText 分钟指示器文本
     */
    public void setIndicatorText(String hourText, String minuteText) {
        mHourPicker.setIndicatorText(hourText);
        mMinutePicker.setIndicatorText(minuteText);
    }

    /**
     * 设置指示器文字的颜色
     * set indicator text color
     *
     * @param textColor 文本颜色
     */
    public void setIndicatorTextColor(int textColor) {
        mHourPicker.setIndicatorTextColor(textColor);
        mMinutePicker.setIndicatorTextColor(textColor);
    }

    /**
     * 设置指示器文字的大小
     * indicator text size
     *
     * @param textSize 文本大小
     */
    public void setIndicatorTextSize(int textSize) {
        mHourPicker.setTextSize(textSize);
        mMinutePicker.setTextSize(textSize);
    }

    /**
     * Sets on date selected listener.
     *
     * @param onTimeSelectedListener the on time selected listener
     */
    public void setOnTimeSelectedListener(OnTimeSelectedListener onTimeSelectedListener) {
        mOnTimeSelectedListener = onTimeSelectedListener;
    }

    /**
     * The interface On date selected listener.
     */
    public interface OnTimeSelectedListener {
        /**
         * On time selected.
         *
         * @param hour   the hour
         * @param minute the minute
         */
        void onTimeSelected(int hour, int minute);
    }
}
